package com.yan.utils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.tuckey.web.filters.urlrewrite.Rule;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.jfinal.json.FastJson;
import com.yan.bean.Blog;
import com.yan.bean.base.BaseBlog;

public class FileUtil {

	public static String getFileRootPath() {
		String path = FileUtil.class.getClass().getResource("/").getPath();
		System.out.println(path);
		return path;

	}

	private static void testMulitiSet() {
		Multiset<Integer> multiSet = HashMultiset.create();
		multiSet.add(10);
		multiSet.add(30);
		multiSet.add(30);
		multiSet.add(40);

		Set<Integer> set = multiSet.elementSet();
		for (Integer data : set) {
			System.out.print(data + "\t");
		}
		System.out.println(multiSet.count(30)); // 2
		System.out.println(multiSet.size()); // 4
	}

	public static void main(String args[]) throws IOException {

		String data = "This is a test.........";

		String path = FileUtil.class.getClass().getResource("/").getPath();

		String path2 = FileUtil.class.getClassLoader().getResource(".").getPath();

		File file = new File(path.substring(0, path.lastIndexOf("/")) + "../../../src/main/resources/test.txt");

		System.out.println(path + "  " + path2 + "  \n" + file.getPath());

		FileUtils.writeStringToFile(file, data, "utf-8");

		System.out.println("over..." + getFileRootPath());

		List<String> list = Lists.newArrayList();
		list.add("1");
		list.add("2");
		list.add("3");

		for (String s : list) {
			System.out.println(s);
		}

		testMulitiSet();

		@SuppressWarnings("rawtypes")
		Multimap<String, BaseBlog> multimap = ArrayListMultimap.create();
		BaseBlog<Blog> baseBlog = new Blog();
		baseBlog.setId(1);
		baseBlog.setContent("this is a test");

		multimap.put("yan", baseBlog);
		multimap.put("yan", baseBlog);

		for (BaseBlog<Blog> blog : multimap.get("yan")) {
			System.out.println(blog);
		}

		// URL url =
		// FileUtil.class.getClass().getProtectionDomain().getCodeSource().getLocation();

		System.out.println(Rule.class.getProtectionDomain().getCodeSource().getLocation().getPath());
		System.out.println(Rule.class.getProtectionDomain().getCodeSource().getLocation().getPath());
	}

}
